package com.qst.crop.demo.controller;

import com.qst.crop.demo.common.Result;
import com.qst.crop.demo.entity.Product;
import com.qst.crop.demo.entity.Question;
import com.qst.crop.demo.entity.User;
import com.qst.crop.demo.service.QuesService;
import com.qst.crop.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("/ques")
@CrossOrigin(origins = "*")
public class QuesController {

    @Autowired
    QuesService quesService;
    @Autowired
    UserService userService;

    /**
     *  增加问题
     * @param email
     * @param title
     * @param description
     * @param imagePath
     * @return
     */
    @RequestMapping("/add")
    public Result add(String email, String title, String description, String imagePath){
        System.out.println("添加问题");

        if(!StringUtils.hasLength(email) || !StringUtils.hasLength(title) || !StringUtils.hasLength(description) || !StringUtils.hasLength(imagePath)){
            return Result.fail(-1, "无效参数");
        }

        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "不存在该用户");
        }

        if(quesService.add(email, title, description, imagePath, new Date(System.currentTimeMillis())) == 1){

            return Result.success(200, "添加成功");
        }else{

            return Result.fail(-3, "添加失败");
        }
    }


    /**
     *  删除问题
     * @param email
     * @param questionId
     * @return
     */
    @RequestMapping("/del")
    public Result delete(String email, Long questionId){
        System.out.println("删除问题");

        if(!StringUtils.hasLength(email) || questionId <= 0){
            return Result.fail(-1, "无效参数");
        }

        User user = userService.getUserByEmail(email);
        if(user == null){
            return Result.fail(-2, "用户不存在");
        }

        if(quesService.deleteById(questionId) == 1){

            return Result.success(200, "删除成功");
        }else{

            return Result.fail(-3, "问题不存在");
        }
    }


    /**
     *  根据id获取问题
     * @param questionId
     * @return
     */
    @RequestMapping("/getQuesById")
    public Result getQuesById(Long questionId){
        System.out.println("通过id获取问题");

        if(questionId <= 0){
            return Result.fail(-1, "无效参数");
        }

        Question question = quesService.getQuesById(questionId);
        if(question == null){

            return Result.fail(-2, "问题不存在");
        }else{

            return Result.success(200, question, "查找成功");
        }
    }

    /**
     *  搜索问题
     * @param searchWord
     * @return
     */
    @RequestMapping("/search")
    public Result select(String searchWord){
        System.out.println("搜索问题");
        if(searchWord == null){
            return Result.fail(-1, "非法请求");
        }

        List<Question> questions = quesService.select(searchWord);
        if(questions.size() == 0){
            return Result.fail(-2, "未找到相关问题");
        }

        return Result.success(200, questions);
    }

}
